<?php

class City_Model_Danhmuctinhthanh extends Zend_Db_Table_Abstract {
	public $_name = "city";
	public function __construct($config = array()) {
		$this->_db = Zend_Db_Table::getDefaultAdapter();
		parent::__construct($config);
	}

	public function count($parameter) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		if ($parameter['name'] != "") {
			$name = $parameter['name'];
			$where .= " and U.NAME LIKE '%" . $name . "%'";
		}
		$sql = "select count(*) as COUNT from " . $this->_name
				. " U WHERE $where";
		$r = $db->query($sql)->fetch();
		return $r['COUNT'];
	}
	public function SelectAll($parameter, $limit, $offset) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		$param = array();
		if ($parameter['name'] != "") {
			$name = $parameter['name'];
			$where .= " and U.NAME LIKE '%" . $name . "%'";
		}
		$sql = "select * from " . $this->_name
				. " U WHERE $where  ORDER BY U.NAME limit $offset,$limit
					 ";
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	
	public function SelectAllCity() {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from " . $this->_name
				. " U ORDER BY U.NAME";
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	
	public function AddNewTower($params) {
		$id = $params['id'];
		$db = Zend_Db_Table::getDefaultAdapter();
		$data = array(
				            'NAME' => $params['name']
		);
		try {
			if($id ==0){
				$db->insert($this->_name, $data);
			}
			else{
				$db->update($this->_name, $data,"CITYID = ".$id);
			}
		}catch (Zend_Exception $e) {
			echo ($e->getMessage());
			exit;
		}
	}
	public function DeleteTower($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = $db->quoteInto('CITYID = ?', $id);
		$db->delete($this->_name, $where);
	}
	public function FindById($id) {
		$result = $this->getDefaultAdapter()
		->query("SELECT * FROM $this->_name WHERE CITYID = ?", $id);
		return $result->fetch();
	}
	public function CheckExitsID($id) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_name where CITYID='" . $id . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
		return 1;
		else
		return 0;
	}
	public function CheckExitsName($name) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_name where NAME='" . $name . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
		return 1;
		else
		return 0;
	}
}

